package com.ktjy.config;

import com.ktjy.cond.ConditionOnPropertyEnable;
import org.apache.commons.dbcp2.BasicDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.*;

import javax.sql.DataSource;

@Configuration
@PropertySource("classpath:database.properties")
public class DataSourceConfig {
    @Value("${jdbc.driver}")
    private String driver;

    @Value("${jdbc.url}")
    private String url;

    @Value("${jdbc.username}")
    private String username;

    @Value("${jdbc.password}")
    private String password;

    //生产环境
    @Bean
    @ConditionOnPropertyEnable(enableProperty = "dev.enable")
    @Conditional(ConditionOnPropertyEnable.OnEnableCondition.class)
    public DataSource dataSource() {
        BasicDataSource dataSource = new BasicDataSource();
        dataSource.setDriverClassName(driver);
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        return dataSource;
    }

    //测试环境
    @Bean
    @ConditionOnPropertyEnable(enableProperty = "test.enable")
    @Conditional(ConditionOnPropertyEnable.OnEnableCondition.class)
    public DataSource dataSource02() {
        BasicDataSource dataSource = new BasicDataSource();
        dataSource.setDriverClassName(driver);
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        return dataSource;
    }
}
